package or.exa.cangguan.service.impl;

import cn.hutool.core.bean.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import or.exa.cangguan.common.Page;
import or.exa.cangguan.entity.dto.GoodsDto;
import or.exa.cangguan.entity.po.Goods;
import or.exa.cangguan.entity.po.InOperation;
import or.exa.cangguan.entity.po.InStore;
import or.exa.cangguan.entity.po.InStoreDetail;
import or.exa.cangguan.entity.vo.GoodsVo;
import or.exa.cangguan.entity.vo.InStoreVo;
import or.exa.cangguan.mapper.*;
import or.exa.cangguan.service.InStoreService;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class InStoreServiceImpl implements InStoreService {

    @Autowired
    private InStoreMapper inStoreMapper;
    @Autowired
    private InStoreDetailMapper inStoreDetailMapper;
    @Autowired
    private StoreMapper storeMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private InOperationMapper inOperationMapper;




//    添加入库
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addInStore(InStoreVo inStoreVo) {
        Date date = new Date(System.currentTimeMillis());
        log.info("inStoreVo:{}",inStoreVo);

        InStore inStore = new InStore();
        BeanUtil.copyProperties(inStoreVo,inStore);
        inStore.setCreateTime(date);
        inStore.setCreateBy(0);
        inStore.setUpdateBy(0);
        inStore.setUpdateTime(date);
        inStore.setInStatus("0"); // 设置初始状态为未入库
        
        // 插入入库单主表
        Integer storeId = storeMapper.selectByName(inStoreVo.getStoreName());
        inStore.setStoreId(storeId);
        int i = inStoreMapper.insertSelective(inStore);
        log.info("inStore:{}",inStore);

        //新增作业单
        InOperation inOperation = new InOperation();
        inOperation.setInId(inStore.getId());
        inOperation.setStatus("0");
        inOperation.setCreateTime(date);
        inOperation.setCreateBy(0);
        inOperation.setUpdateBy(0);
        inOperation.setUpdateTime(date);

        inOperationMapper.insertSelective(inOperation);



        // 获取商品信息
        List<Goods> goodsList = goodsMapper.selectByName(inStoreVo.getGoodsList());
        
        // 插入入库单明细
        int detailCount = 0;
        for (GoodsVo goodsVo : inStoreVo.getGoodsList()) {
            InStoreDetail inStoreDetail = new InStoreDetail();
            inStoreDetail.setInId(inStore.getId());
            // 根据商品名称找到对应的商品ID
            for (Goods goods : goodsList) {
                if (goods.getGoodsName().equals(goodsVo.getGoodsName())) {
                    inStoreDetail.setGoodsId(goods.getGoodsId());
                    break;
                }

            }
            inStoreDetail.setGoodsCount(goodsVo.getGoodsCount());
            inStoreDetail.setCreateTime(date);
            inStoreDetail.setUpdateTime(date);
            inStoreDetail.setCreateBy(0);
            inStoreDetail.setUpdateBy(0);

            detailCount += inStoreDetailMapper.insertSelective(inStoreDetail);
        }

        return i > 0 && detailCount == inStoreVo.getGoodsList().size() ? 1 : 0;
    }


    //更新入库单
    @Override
    public int updateInStore(InStoreVo inStoreVo) {
        Date date = new Date(System.currentTimeMillis());
        InStore inStore = new InStore();
        BeanUtil.copyProperties(inStoreVo,inStore);
        inStore.setUpdateBy(0);
        inStore.setUpdateTime(date);
        Integer storeId = storeMapper.selectByName(inStoreVo.getStoreName());
        inStore.setStoreId(storeId);

        int i = inStoreMapper.updateByPrimaryKeySelective(inStore);

        List<Goods> goodsList = goodsMapper.selectByName(inStoreVo.getGoodsList());
        log.info("inStore.getId():"+inStore.getId());
        inStoreDetailMapper.deleteByPrimaryKey(inStore.getId());
        int detailCount = 0;
        for (GoodsVo goodsVo : inStoreVo.getGoodsList()) {
            InStoreDetail inStoreDetail = new InStoreDetail();
            inStoreDetail.setInId(inStore.getId());
            // 根据商品名称找到对应的商品ID
            for (Goods goods : goodsList) {
                if (goods.getGoodsName().equals(goodsVo.getGoodsName())) {
                    inStoreDetail.setGoodsId(goods.getGoodsId());
                    break;
                }

            }
            inStoreDetail.setGoodsCount(goodsVo.getGoodsCount());
            inStoreDetail.setCreateTime(date);
            inStoreDetail.setUpdateTime(date);
            inStoreDetail.setCreateBy(0);
            inStoreDetail.setUpdateBy(0);

            detailCount += inStoreDetailMapper.insertSelective(inStoreDetail);
        }




        return i > 0 && detailCount == inStoreVo.getGoodsList().size() ? 1 : 0;
    }



    //查询入库单
    @Override
    public Page searchInStore(InStoreVo inStoreVo) {

        Page page = new Page();


        Integer storeId = null;
        log.info("----------------------"+inStoreVo);
        if(inStoreVo.getStoreName() != null && inStoreVo.getStoreName() != ""){
            storeId= storeMapper.selectByName(inStoreVo.getStoreName());
        }
        BeanUtil.copyProperties(inStoreVo, page);
         page.setResultList(inStoreMapper.selectInStore(storeId, inStoreVo.getInNumber(), inStoreVo.getInPlanTime(),page));;

        Integer totalNum = inStoreMapper.selectInStoreCount(storeId, inStoreVo.getInNumber(), inStoreVo.getInPlanTime());
        page.setTotalNum(totalNum);
        BeanUtil.copyProperties(inStoreVo,page);


        return page;
    }


    //删除入库单
    @Override
    public int deleteInStore(Integer id) {
        int i= inStoreMapper.deleteByPrimaryKey(id);
        int j = inStoreDetailMapper.deleteByPrimaryKey(id);


        return i+j;
    }


    //查询入库单商品
    @Override
    public List<GoodsDto> searchInStoreGoods(Integer id) {


        List<GoodsDto> goodsDtos = inStoreDetailMapper.selectByPrimaryKey(id);
        log.info("goodsDtos:"+goodsDtos);;

        return goodsDtos;
    }


//更新状态
    @Override
    public int updateStatus(Integer id, String status, Date inPlanTime) {

        if (status.equals("2")) {
            inOperationMapper.deleteByInId(id);

        }

        int i = inStoreMapper.updateInStatus(id, status, inPlanTime);
        return i;
    }


}
